import 'package:ball_lion/entity/expert/plan_Info_entity.dart';
import 'package:ball_lion/entity/expert/plan_form/plan_form_detail_entity.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/utils/utils.dart';

class PlanFormList {
  IssueInfo? current;
  List<IssueInfo>? history;
  List<Mark>? mark;
  String? maxRed;
  String? profit;
  String? profitTitle;
  String? tag;

  PlanFormList(
      {this.current,
      this.history,
      this.mark,
      this.maxRed,
      this.profit,
      this.profitTitle,
      this.tag});

  PlanFormList.fromJson(Map<String, dynamic> json) {
    if (json['current'] != null) {
      current = new IssueInfo.fromJson(json['current']);
    }
    if (json['history'] != null) {
      history = <IssueInfo>[];
      json['history'].forEach((v) {
        history!.add(new IssueInfo.fromJson(v));
      });
    }
    if (json['mark'] != null) {
      mark = <Mark>[];
      json['mark'].forEach((v) {
        mark!.add(new Mark.fromJson(v));
      });
    }
    maxRed = json['maxRed'];
    profit = json['profit'];
    profitTitle = json['profitTitle'];
    tag = json['tag'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.current != null) {
      data['current'] = this.current!.toJson();
    }
    if (this.history != null) {
      data['history'] = this.history!.map((v) => v.toJson()).toList();
    }
    if (this.mark != null) {
      data['mark'] = this.mark!.map((v) => v.toJson()).toList();
    }
    data['maxRed'] = this.maxRed;
    data['profit'] = this.profit;
    data['profitTitle'] = this.profitTitle;
    data['tag'] = this.tag;
    return data;
  }
}

class IssueForm {
  List<String>? stage;
  List<int>? spot;
  List<String>? matches;
  List<String>? jcMatchNo;
  List<List<Choice>>? choices;
  List<String>? time;

  IssueForm({this.spot, this.matches, this.choices, this.time, this.jcMatchNo});

  List<StageLine> get lines => List.generate(spot?.length ?? 0, (index) {
        int start = index == 0 ? 0 : spot![index - 1];
        return StageLine(
            stage: stage?[index] ?? "",
            choice: choices?.sublist(start, spot?[index]),
            match: matches?.sublist(start, spot?[index]),
            jcMatchNo: jcMatchNo?.sublist(start, spot?[index]),
            time: time?.sublist(start, spot?[index]));
      });

  IssueForm.fromInfo(List<IssueDetail> entity) {
    if (entity.isEmpty) return;
    stage = [];
    spot = [];
    matches = [];
    jcMatchNo = [];
    choices = [];
    time = [];
    var count = 0;
    for (int i = 1; i <= entity.length; i++) {
      Iterable<IssueDetail> detail =
          entity.where((element) => element.stage == i);
      for (var element in detail) {
        stage?.add(element.stage.toChn);
        count += element.matchBries?.length ?? 0;
        spot?.add(count);
        List<List<PlanItemShowEntity>> showList = [];
        element.matchBries?.forEach((match) {
          String team = match.sportsId == 1
              ? "${match.homeName} vs ${match.guestName}"
              : "${match.guestName} vs ${match.homeName}";
          String matchTime =
              DateTime.tryParse(match.matchTime ?? "")?.formatedString("MM-dd");
          var list = element.itemShow
                  ?.where((e) => e.matchId == match.matchId)
                  .toList() ??
              [];
          matches?.add(team);
          jcMatchNo?.add(match.jcMatchNo.hasValue ? "${match.jcMatchNo} " : "");
          time?.add(matchTime);
          showList.add(list);
        });
        choices?.addAll(showList.map((e) => element.state == 4
            ? [Choice(choice: "取消", aim: false)]
            : _getChoice(e)));
      }
    }
  }

  List<Choice> _getChoice(List<PlanItemShowEntity> list) {
    List<Choice> choice = [];
    for (var element in list) {
      switch (element.playType) {
        case 1001:
          var list =
              element.spf?.list?.where((element) => (element.s ?? 0) > 0);
          choice.addAll(
              list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ?? []);
          break;
        case 1002:
          element.rqSpf?.forEach((element) {
            var list = element.list?.where((element) => (element.s ?? 0) > 0);
            choice.addAll(
                list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ??
                    []);
          });
          break;
        case 1004:
          var list =
              element.jqs?.list?.where((element) => (element.s ?? 0) > 0);
          choice.addAll(
              list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ?? []);
          break;
        case 1005:
          var list =
              element.bqc?.list?.where((element) => (element.s ?? 0) > 0);
          choice.addAll(
              list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ?? []);
          break;
        case 1006:
          element.yp?.forEach((element) {
            var list = element.list?.where((element) => (element.s ?? 0) > 0);
            choice.addAll(
                list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ??
                    []);
          });
          break;
        case 1007:
          element.dx?.forEach((element) {
            var list = element.list?.where((element) => (element.s ?? 0) > 0);
            choice.addAll(
                list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ??
                    []);
          });
          break;
        case 1008:
          element.sp?.forEach((element) {
            var list = element.list?.where((element) => (element.s ?? 0) > 0);
            choice.addAll(
                list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ??
                    []);
          });
          break;
        case 2001:
          var list =
              element.lqSf?.list?.where((element) => (element.s ?? 0) > 0);
          choice.addAll(
              list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ?? []);
          break;
        case 2002:
          element.lqRqSf?.forEach((element) {
            var list = element.list?.where((element) => (element.s ?? 0) > 0);
            choice.addAll(
                list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ??
                    []);
          });
          break;
        case 2003:
          element.lqDx?.forEach((element) {
            var list = element.list?.where((element) => (element.s ?? 0) > 0);
            choice.addAll(
                list?.map((e) => Choice(choice: e.n ?? "", aim: e.r == 1)) ??
                    []);
          });
          break;
      }
    }
    return choice;
  }
}

class StageLine {
  String stage;
  List<String>? time;
  List<String>? match;
  List<String>? jcMatchNo;
  List<List<Choice>>? choice;

  StageLine(
      {required this.stage,
      this.time,
      this.match,
      this.choice,
      this.jcMatchNo});
}

class Choice {
  String choice;
  bool aim;

  Choice({required this.choice, required this.aim});
}

class Mark {
  int? state;
  int? issue;
  int? issueId;
  String? title;

  Mark({this.state, this.issue, this.issueId, this.title});

  Mark.fromJson(Map<String, dynamic> json) {
    state = json['state'];
    issue = json['issue'];
    issueId = json['issueId'];
    title = json['title'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['state'] = this.state;
    data['issue'] = this.issue;
    data['issueId'] = this.issueId;
    data['title'] = this.title;
    return data;
  }
}
